package hiho.ku._1014;

import java.util.Scanner;

public class Main {
	
	private static class Node {
		
		private int count;
		private Node[] kids;
		
		public Node() {
			count = 0;
			kids = new Node[26];
		}
		
		public void push(String s) {
			count++;
			if (s.length() <= 0)
				return;
			int head = s.charAt(0) - 'a';
			if (kids[head] == null)
				kids[head] = new Node();
			kids[head].push(s.substring(1));
		}
		
		public Node getKid(int i) {
			return kids[i];
		}
		
		public int getCount() {
			return count;
		}
		
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		
		Node root = new Node();
		
		int n = in.nextInt();
		for (int i = 0; i < n; i++) {
			String s = in.next();
			root.push(s);
		}
		
		int m = in.nextInt();
		for (int i = 0; i < m; i++) {
			String s = in.next();
			Node now = root;
			for (int j = 0; j < s.length(); j++) {
				int head = s.charAt(j) - 'a';
				now = now.getKid(head);
				if (now == null)
					break;
			}
			if (now != null)
				System.out.println(now.getCount());
			else
				System.out.println(0);
		}
	}

}
